// +----------------------------------------------------------------------
// | Project:   Commons  
// +----------------------------------------------------------------------
// | CreateTime: 15/11/3  下午6:51
// +----------------------------------------------------------------------
// | Author:     xab(xab@xabad.cn)
// +----------------------------------------------------------------------
// | Description:
// +----------------------------------------------------------------------
package cn.xabad.commons.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import butterknife.ButterKnife;

/**
 * DESC   :
 * AUTHOR : Xabad
 */
public abstract class CommFragment extends Fragment implements CommViewInferface {
    public Context context;
    public Activity activity;
    protected View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getArgs(getArguments());
        rootView = inflater.inflate(setView(), null);
        ButterKnife.bind(this, rootView);
        activity = getActivity();
        context = activity.getApplicationContext();
        OttoManager.register(this);
        setting();
        initView();
        setListener();
        return rootView;
    }


    protected abstract void getArgs(Bundle bundle);

    protected abstract int setView();

    /**
     * 初始化布局文件中的控件
     */
    public abstract void initView();

    public abstract void setting();

    public abstract void setListener();

    protected void startActivity(Class cls) {
        startActivity(cls, null);
    }

    protected void startActivity(Class cls, Bundle bundle) {
        if (cls == null || activity == null || activity.isFinishing()) return;
        Intent intent = new Intent();
        if (bundle != null)
            intent.putExtras(bundle);
        intent.setClass(activity, cls);
        startActivity(intent);
    }

    @Override
    public void onDestroyView() {
        super.onDestroy();
        ButterKnife.unbind(this);
        OttoManager.unregister(this);
    }

}
